<?php
declare (strict_types=1);

namespace app\service;



class SiteService
{
    public function getBanner()
    {
        $time = date('Y-m-d H:i:s');
        $list = DataSetBanner::where('address', '首页顶部')
            ->where('s_time','<=',$time)
            ->where('e_time','>=',$time)
            ->where(siteWhere())
            ->where('status',1)
            ->order('sort')
            ->select()->toArray();
        return $list;
    }

    public function getSeekCate()
    {
        return DataSeekCate::where('pid', 0)->where('deleted',0)->order('sort', 'asc')->select()->toArray();
    }

    public function getSeekConsultant($catId, $priceId, $typeId, $groupId, $cityId)
    {
        $where = [];
        if ($catId) {
            //$where[] = ['realm', '=', $catId];
            $where[] = ['realm', 'like', "%{$catId}%"];
        }
        if ($priceId) {
            $where[] = ['consultation_price', '=', $priceId];
        }
        if ($typeId) {
            $where[] = ['seek_way', 'like', "%{$typeId}%"];
        }
        if ($groupId) {
            //$where[] = ['consultation_group', '=', $groupId];
            $where[] = ['consultation_group', 'like', "%{$groupId}%"];
        }
        if ($cityId) {
            //$where[] = [$cityId, '=', 1];
            $where[] = ['affiliation_city', '=', $cityId];
        }
        return DataSeekConsultant::where($where)->where('status', 1)->order('sort')->paginate(
            [
                'list_rows'=> 20,
                'query'=> ['city_id'=>$cityId],
            ]
        );
    }

    public function getSeekCateList()
    {
        $oneCate = DataSeekCate::where('pid', 0)->where('deleted',0)->limit(8)->order('sort', 'asc')->select()->toArray();
        $oneCateId = array_column($oneCate, 'id');

        $consultant = DataSeekConsultant::where('switch', 'on')
            ->where('status', 1)->whereIn('realm', $oneCateId)->select()->toArray();
        foreach ($oneCate as $key => $item) {
            foreach ($consultant as $k => $v) {
                if ($item['id'] == $v['realm']) {
                    $oneCate[$key]['consultant'][] = $v;
                }
            }
        }
        return $oneCate;
    }

    public function consultant($id)
    {
        return DataSeekConsultant::where('status', 1)->where('id',$id)->find();
    }

    public function getEvaluation()
    {
        return DataEvaluation::where('status', 1)->where('is_index', 'on')->select()->toArray();
    }

    public function getInformationCateList()
    {
        $oneCate = DataInformationCate::where('pid', 0)->where('status', 1)->where('deleted',0)->select()->toArray();
        $oneCateId = array_column($oneCate, 'id');

        $information = DataInformation::where('cate_id', 'in', $oneCateId)->where('status', 1)->order('sort')->select()->toArray();
        foreach ($oneCate as $key => $item) {
            foreach ($information as $k => $v) {
                if ($item['id'] == $v['cate_id']) {
                    $oneCate[$key]['information'][] = $v;
                }
            }
        }
        return $oneCate;
    }

    public function getTrends()
    {
        $list = DataTrends::where('status', 1)->where('is_index', 'on')->select()->toArray();
        return $list;
    }

    public function getSeekConfig()
    {
        $config = DataSystemConfig::whereIn('type', [2, 3, 5])->select()->toArray();
        $data = [];
        foreach ($config as $item) {
            $data[$item['type']][] = $item;
        }
        return $data;
    }
}
